// 锁屏意图, 配合 Tasker 完成锁屏动作
const ACTION_LOCK_SCREEN = "autojs.intent.action.LOCK_SCREEN"

var deviceNotLockedCount = 0
setInterval(() => {
    let result = isDeviceLocked()
    console.log('screen lock status:',result)
    if(!result)
    {
        deviceNotLockedCount++;
    }else{
        deviceNotLockedCount = 0;
    }
    //超过2次超时未锁屏，强制锁屏
    if(deviceNotLockedCount > 1 )
    {
        console.log(textMatches("上滑解锁").findOne(2000))
        console.log('current deviceNotLockedCount:',deviceNotLockedCount)
        if(null != textMatches("上滑解锁").findOne(2000))
        {
            //发送锁屏意图
            app.sendBroadcast({ action: ACTION_LOCK_SCREEN });
        }else{
            //重置计数
            deviceNotLockedCount = 0;
        }
        
    }
}, 45 * 1000);

// 屏幕是否为锁定状态
function isDeviceLocked() {
    importClass(android.app.KeyguardManager)
    importClass(android.content.Context)
    var km = context.getSystemService(Context.KEYGUARD_SERVICE)
    return km.isKeyguardLocked()
}